package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.TypePop
import hlhj.fhp.supreme.javabean.ClassListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_class_filter_aty.*
import java.util.ArrayList

class ClassFilterAty : BaseAty(), TypePop.OnTypeSelectListener {
    private lateinit var cid:String
    private var level_id:String="1"
    private lateinit var typePop:TypePop
    private val levelDatas=ArrayList<ClassListBean.DataBeanX.LevelBean>()
    private lateinit var adp:BaseQuickAdapter<ClassListBean.DataBeanX.DataBean,BaseViewHolder>
    private val datas=ArrayList<ClassListBean.DataBeanX.DataBean>()
    override fun getContentId(): Int {
        return R.layout.activity_class_filter_aty
    }


    override fun initView() {
        cid=intent.getStringExtra("cat_id")
        level_id=intent.getStringExtra("lid")
        when(level_id){
            "1"->{
                tvYear.text="请选择"
            }
            "2"->{
                tvYear.text="请选择"
            }
            "3"->{
                tvYear.text="请选择"
            }
        }
        tv1.text=intent.getStringExtra("tittle")
        adp=object :BaseQuickAdapter<ClassListBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.jiepan_list_item,datas){
            override fun convert(helper: BaseViewHolder?, item: ClassListBean.DataBeanX.DataBean?) {
                Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.price,item?.money)
                helper?.setText(R.id.itemTime,MyUtils.timeStampToStrYMD(item?.create_time!!.toLong()))
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        dispatchData()


    }

    private fun dispatchData() {
        OkGo.post<ClassListBean>(Urls.BaseUrl + Urls.CLASS_LIST)
                .params("cat_id", cid)
                .params("level_id", level_id)
                .execute(object : JsonCallBack<ClassListBean>(this) {
                    override fun onSuccess(response: Response<ClassListBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            if (bean?.data[0].data!=null){
                                datas.addAll(bean?.data[0].data)
                                levelDatas.addAll(bean?.data[0].level)
                                typePop = TypePop(this@ClassFilterAty, levelDatas, this@ClassFilterAty)
                                adp.notifyDataSetChanged()
                            }

                        }
                    }
                })
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }

        tvYear.setOnClickListener {
            if (typePop.isShowing){
                typePop.dismiss()
            }else{
                typePop.showAsDropDown(tvYear)
            }
        }
        adp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, ClassDetailAty::class.java)
            intent.putExtra("id",datas[position].id)
            startActivity(intent)
        }
    }

    override fun onTypeSelect(id: String, str: String) {
        level_id=id
        tvYear.text=str
        typePop.dismiss()
        datas.clear()
        levelDatas.clear()
        dispatchData()

    }
}
